import { observable } from "mobx";
import { check } from "@/utils/check";
import apiAxios from "@/utils/requestAxios";
import api from "../utils/api"
import GlobalModel from "@/utils/static";
import { message } from 'antd';
import fingerprintApi from "./finger_device/model"

const store = observable({
    loading: false,
    // 程序加载进度条
    state: 5,
    // 监听关闭窗口事件
    isAddCloseConfirm: false,
    // 检测指纹机状态计时器
    timer: null,
    // 字典list
    dict: [],
    // 网络错误
    network: false,

    //登录  //获取token
    async login() {
        store.loading = true
        const parms = {
            username: "admin",
            password: "123456",
        };
        const res = await apiAxios('POST', `${store.HTTP}${api.loginByWeb}`, parms);
        store.loading = false
        if (!check(res)) return; //进行检测验证
        message.success('登录成功');
        console.log(res.data)
        GlobalModel.SetTokenData(res.data);
    },
    // 下载指纹文件(已经有指纹数据就不要下载了)
    async fingerprintFile() {
        if (!sessionStorage.getItem("isFingerPrintFile")) {
            let IP = JSON.parse(localStorage.getItem("device"))?.IP
            // 没有设置IP就不用下载指纹文件
            if (IP) {
                //设置加载的标记A,没有进度,只有loaded,查询不到total需要后端优化
                let res = await apiAxios("GET", api.fingerprintFile(), {}, {
                    responseType: "blob",
                    // onDownloadProgress: progressEvent => {
                    //     //const percentCompleted = Math.round((progressEvent.loaded * 100) / progressEvent.total);
                    //     console.log(`Download progress: `,progressEvent.loaded,progressEvent.total);
                    // }
                })
                console.log("res====", res)
                // 如果接口报错给出提示
                if (!res || (res.status != 200)) {
                    console.log("网络错误", res)
                    store.network = true
                    store.state = 100
                }
                if (res.data) {
                    //设置加载的标记A,这个时候已经下载完成
                    console.log("指纹数据===", res.data)
                    store.state = 8
                    // 导入文件
                    fingerprintApi.writeBlobFile("config.txt", res.data)
                    // // 查看进程
                    // fingerprintApi.openExe()
                    // 村本地sessionStorage
                    sessionStorage.setItem("isFingerPrintFile", true)
                }
            }
        }
    },
    // 当日指纹信息入库
    async dayFingerprint() {
        let res = await apiAxios("GET", api.dayFingerprint(), {})
        if (res.data && res.data.length) {
            console.log("当日指纹数据===", res.data)
            let dayfingerprintData = JSON.parse(sessionStorage.getItem("dayfingerprintData"))
            // 判断本地有没有存 “dayfingerprintData” 数据，没有就是第一次要全部导入
            if (dayfingerprintData) {
                // dayfingerprintData 不为空数组
                if (dayfingerprintData.length) {
                    res.data.forEach((item) => {
                        let flag = true
                        dayfingerprintData.forEach((subItem) => {
                            // 筛选不相同的
                            if (item.fingerprintStr === subItem) {
                                flag = false
                            }
                        })
                        if (flag) {
                            fingerprintApi.WEB_FINGER_ADD(item.patientId, item.fingerprintStr)
                        }
                    })
                }
            } else { // 第一次
                res.data.map((item) => {
                    fingerprintApi.WEB_FINGER_ADD(item.patientId, item.fingerprintStr)
                })
            }
        }
    },
    // 进入对应路由，记录路由路径到localStorage
    StoragePath(path) {
        localStorage.setItem("endPath", path)
    },

    //播放提示音频
    playAudio() {
        let ass = require("../assets/ding.mp3")
        const audio = new Audio(ass.default)
        audio.play().then()
    },
    // 获取字典
    async getDictList() {
        let res = await apiAxios("POST", api.getDict(), { "size": 2000, "current": 1 })
        if (res && res.data && res.data.records && res.data.records.length) {
            store.dict = res.data.records
        }
    },

})

export default store